/**
 * 
 */
package org.t2.backstage.module.hotfix.service;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.banish.sql.core.util.QuerySet;
import org.gaming.backstage.PageData;
import org.gaming.ruler.schedule.LoopingTask;
import org.gaming.service.advice.ResponseResult;
import org.gaming.service.service.AbstractService;
import org.gaming.tool.DateTimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.t2.backstage.module.gameserver.model.GameServer;
import org.t2.backstage.module.gameserver.service.GameServerService;
import org.t2.backstage.module.hotfix.model.FixDesign;
import org.t2.backstage.module.hotfix.model.FixDesignResult;
import org.t2.backstage.module.hotfix.struct.FixDesignTask;
import org.t2.backstage.util.T2HttpUtil;

import sh.t2.share.apiutil.GameApi;

/**
 * @author YY
 *
 */
@Service
public class FixDesignService extends AbstractService<FixDesign> {

	private static Logger logger = LoggerFactory.getLogger(FixDesignService.class);
	
	@Autowired
	private GameServerService gameServerService;
	@Autowired
	private FixDesignResultService fixDesignResultService;
	@Autowired
	private HotfixService hotfixService;
	
	private Queue<FixDesignTask> taskQueue = new ConcurrentLinkedQueue<>();
	
	public PageData<FixDesign> query(QuerySet querySet) {
		List<FixDesign> list = this.repository().getBaseDao().queryListWhere(querySet.getWhere(), querySet.getParams());
		
		PageData<FixDesign> pageData = new PageData<>();
		pageData.setCount(this.repository().getBaseDao().countWhere(querySet.getCountWhere(), querySet.getCountParams()));
		pageData.setData(list);
		return pageData;
	}
	
	public FixDesign getEntity(long id) {
		return this.repository().getByKeys(id);
	}
	
	public void addTask(FixDesignTask task) {
		taskQueue.add(task);
	}
	
	@LoopingTask(name = "FixDesignService-热更配置", spaceTime = DateTimeUtil.ONE_SECOND_MILLIS * 10, execGroup = 2)
	private void runInSchedule() {
		FixDesignTask task = taskQueue.poll();
		if(task == null) {
			return;
		}
		
		List<File> files = new ArrayList<>();
		for(String filePath : task.getFixDesign().getFileNames()) {
			File file = new File(hotfixService.getHotfixDesignFolder() + filePath);
			files.add(file);
		}
		Map<String, Object> params = new HashMap<>();
		params.put("fixTime", task.getFixDesign().getFixTime());
		
		List<FixDesignResult> updateList = new ArrayList<>();
		for(FixDesignResult result : task.getResults()) {
			if(result.getStatus() == 1) {
				continue;
			}
			GameServer gameServer = gameServerService.getEntity(0, result.getServerUid());
			
			logger.info("正在向服务器{}-{}-{}发送热更配置请求", gameServer.getId(), gameServer.getServerId(), gameServer.getName());
			ResponseResult<String> serverResponse = T2HttpUtil.postParamAndFile(gameServer, GameApi.fixDesignUpload, params, files);
			
			if(serverResponse == null) {
				result.setStatus(2);
				result.setMessage(String.format("服务器%s-%s-%s没有响应", gameServer.getId(), gameServer.getServerId(), gameServer.getName()));
			} else {
				if(serverResponse.getCode() == 0) {
					result.setStatus(1);
					result.setCode(serverResponse.getCode());
					result.setMessage(serverResponse.getMessage());
				} else {
					result.setStatus(2);
					result.setCode(serverResponse.getCode());
					result.setMessage(serverResponse.getMessage());
				}
				logger.info("收到向服务器{}-{}-{}发送热更配置的响应，响应码：{}，响应信息：{}", gameServer.getId(), gameServer.getServerId(),
						gameServer.getName(), serverResponse.getCode(), serverResponse.getMessage());
			}
			updateList.add(result);
		}
		fixDesignResultService.updateAll(updateList);
	}

	@Override
	public void mergeServer(int mergeDbZoneId) {
		//不必理会
	}
}
